package cn.alcode.cardservice.controller;


import cn.alcode.cardservice.model.PageRequestParam;
import cn.alcode.cardservice.model.Student;
import cn.alcode.cardservice.model.TransactionModel;
import cn.alcode.cardservice.service.EcCardService;
import cn.alcode.cardservice.util.AjaxResult;
import cn.alcode.cardservice.util.AjaxUtils;
import cn.alcode.cardservice.util.Func_T;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 卡德智能一卡通接口
 */
@Controller
@RequestMapping("/ecCard")
public class EcCardController {
    @Autowired
    EcCardService ecCardService;

    /**
     * 添加部门
     * @param name
     * @param parentDid
     * @return
     */
    @RequestMapping("/addOrg")
    @ResponseBody
    public AjaxResult<Long> addOrg(String name, String parentDid) {
        return AjaxUtils.process(new Func_T<Long>() {
            @Override
            public Long invoke() throws Exception {
                return ecCardService.doAddOrg(name, parentDid);
            }
        });
    }

    /**
     * 更新部门
     * @param name
     * @param did
     * @param parentDid
     * @return
     */
    @RequestMapping("/updateOrg")
    @ResponseBody
    public AjaxResult<Boolean> updateOrg(String name, String did, String parentDid) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                ecCardService.doUpdateOrg(name, did, parentDid);
                return true;
            }
        });
    }

    /**
     * 添加人员
     * @param student
     * @return
     */
    @RequestMapping("/addPerson")
    @ResponseBody
    public AjaxResult<Long> addPerson(Student student) {
        return AjaxUtils.process(new Func_T<Long>() {
            @Override
            public Long invoke() throws Exception {
                return ecCardService.doAddPerson(student);
            }
        });
    }

    /**
     * 更新人员
     * @param student
     * @return
     */
    @RequestMapping("/updatePerson")
    @ResponseBody
    public AjaxResult<Boolean> updatePerson(Student student) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                ecCardService.doUpdatePerson(student);
                return true;
            }
        });
    }

    /**
     * 添加充值记录
     * @param qcls
     * @param studentCid
     * @param totalAmount
     * @param type
     * @param qdate
     * @return
     */
    @RequestMapping("/addRechargeRecord")
    @ResponseBody
    public AjaxResult<Integer> addRechargeRecord(String qcls, String studentCid, String totalAmount, String type, String qdate) {
        return AjaxUtils.process(new Func_T<Integer>() {
            @Override
            public Integer invoke() throws Exception {
                return ecCardService.doAddRechargeRecord(qcls, studentCid, totalAmount, type, qdate);
            }
        });
    }

    /**
     * 查询交易记录
     * @param page
     * @param studentId
     * @return
     */
    @RequestMapping("/transactionList")
    @ResponseBody
    public AjaxResult<JSONObject> transactionList(PageRequestParam page, String studentId) {
        return AjaxUtils.process(new Func_T<JSONObject>() {
            @Override
            public JSONObject invoke() throws Exception {
                return ecCardService.queryTransaction(page, studentId);
            }
        });
    }

    /**
     * 余额查询
     * @param cid
     * @return
     */
    @RequestMapping("/studentBalance")
    @ResponseBody
    public AjaxResult<String> studentBalance(String cid) {
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                if(!StringUtils.isEmpty(cid)) {
                    return ecCardService.queryBalance(cid);
                }

                return "0";
            }
        });
    }
}
